package cn.gcjxsc.common.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author pumbaa
 * @since 1.0
 */
public class ValidatorUtil {

    private static Validator validator;

    public static Validator getValidator(){
        if (null == validator) {
            validator = Validation.buildDefaultValidatorFactory().getValidator();
        }
        return validator;
    }

    public static  <T> String validate(T obj) {
        Set<ConstraintViolation<T>> validate = getValidator().validate(obj);
        if (validate.size() > 0) {
            return validate.iterator().next().getMessage();
        }
        return null;
    }
}
